{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基础对象\n",
    "\n",
    "- int: 整数，如1, 2, 3\n",
    "\n",
    "\n",
    "- float: 浮点数，如2.3, 3.6\n",
    "\n",
    "\n",
    "- bool: 布尔，包括True, False\n",
    "\n",
    "\n",
    "- str: 字符串，如'12.3', 'abcd'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'float'>\n",
      "<class 'bool'>\n",
      "<class 'str'>\n",
      "hello\n"
     ]
    }
   ],
   "source": [
    "print(type(1))\n",
    "print(type(1.2))\n",
    "print(type(True))\n",
    "print(type('123'))\n",
    "print(\"hello\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 变量及运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串变量\n",
    "\n",
    "#### 常用字符串变量函数\n",
    "\n",
    "+ print\n",
    "+ .title()\n",
    "+ .upper()\n",
    "+ .lower()\n",
    "+ 连接符: +\n",
    "+ 换行符：\\n\n",
    "+ 空格符：\\\n",
    "+ 制表符：\\t\n",
    "+ 去空格：.lstrip(), .rstrip(),.strip()\n",
    "+ 分词：.split\n",
    "+ 判断是否字串形式数字：.isdigit()\n",
    "+ 转为整数：int()\n",
    "+ 接收用户输入:input()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_string = \"This is a double-quoted string.\"\n",
    "print(my_string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('123' + 'HELLO')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Hello everyone!\")\n",
    "print(\"Hello \\teveryone!\")\n",
    "print(\"\\tHello everyone!\")\n",
    "\n",
    "print(\"\\nHello everyone!\")\n",
    "print(\"Hello \\neveryone!\")\n",
    "print(\"Hello \\n\\n\\neveryone!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = ' eric '\n",
    "\n",
    "print(name.lstrip())\n",
    "print(name.rstrip())\n",
    "print(name.strip())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数字变量\n",
    "\n",
    "#### 常用数字变量函数\n",
    "\n",
    "+ 一般运算：+、-、*、/\n",
    "+ 整数除法://\n",
    "+ 取余：%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "1\n",
      "6\n",
      "1.5\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "print(3+2)\n",
    "print(3-2)\n",
    "print(3*2)\n",
    "print(3/2)\n",
    "print(3**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符和数字混排\n",
    "\n",
    "+ 占位符:%s、%d、%f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 / 2 = 2.5\n"
     ]
    }
   ],
   "source": [
    "print(\"5 / 2 = %s\" %(5 / 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 逻辑判断\n",
    "+ 等于（==）\n",
    "+ 不等于（!=）\n",
    "+ 其他不等号（>,>=,<,<=）\n",
    "+ and\n",
    "+ or"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 == 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 == 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4 < 3 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Eric' == 'eric'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "x = 2\n",
    "print (x >= -1 and x>3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 控制流\n",
    "\n",
    "### 条件控制\n",
    "\n",
    "    if <condition>:\n",
    "        do()\n",
    "    elif <condition>:\n",
    "        do_other()\n",
    "    else:\n",
    "        do_another()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a小于等于1\n"
     ]
    }
   ],
   "source": [
    "a  = 0\n",
    "\n",
    "if a > 2 :\n",
    "    print(\"a大于2\")\n",
    "elif a > 1 :\n",
    "    print(\"a小于等于2，大于1\")\n",
    "else:\n",
    "    print(\"a小于等于1\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 循环\n",
    "\n",
    "#### while\n",
    "\n",
    "- while 循环\n",
    "\n",
    "```\n",
    "    while <condition>:\n",
    "        do()\n",
    "        update_condition()\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2550\n"
     ]
    }
   ],
   "source": [
    "sum = 0\n",
    "i = 0\n",
    "\n",
    "while i <= 100:\n",
    "    if i % 2 == 0:\n",
    "        sum += i         \n",
    "    else:\n",
    "        pass    \n",
    "    i += 1\n",
    "\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### for循环\n",
    "\n",
    "```\n",
    "    for x in <iterator>:\n",
    "        do()\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alice, that was a great trick!\n",
      "I can't wait to see your next trick, Alice.\n",
      "\n",
      "Thank you everyone, that was a great magic show!\n",
      "David, that was a great trick!\n",
      "I can't wait to see your next trick, David.\n",
      "\n",
      "Thank you everyone, that was a great magic show!\n",
      "Carolina, that was a great trick!\n",
      "I can't wait to see your next trick, Carolina.\n",
      "\n",
      "Thank you everyone, that was a great magic show!\n"
     ]
    }
   ],
   "source": [
    "magicians = ['alice', 'david', 'carolina'] \n",
    "for magician in magicians: \n",
    "    print(magician.title() + \", that was a great trick!\")  \n",
    "    print(\"I can't wait to see your next trick, \" + magician.title() + \".\\n\") \n",
    "    \n",
    "    print(\"Thank you everyone, that was a great magic show!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 控制异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:  \n",
    "    语句1  \n",
    "    语句2  \n",
    "    .  \n",
    "    .  \n",
    "    语句N  \n",
    "except .........:  \n",
    "    do something .......  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def isFloat(string):\n",
    "    is_float=1\n",
    "    try:\n",
    "        float(string)\n",
    "    except:\n",
    "        is_float=0\n",
    "    return is_float\n",
    "\n",
    "GPA = input(\"请输入你的加权分数，要求精确到小数点后1位 \")\n",
    "\n",
    "\n",
    "while isFloat(GPA) == 0:\n",
    "    GPA = input(\"请输入阿拉伯数字形式的加权分数，要求精确到小数点后1位 \")\n",
    "    \n",
    "GPA = float(GPA)    \n",
    "print(type(GPA))\n",
    "print(GPA)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据结构"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### list\n",
    "\n",
    "+ python使用方括号指明列表，students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "+ 也可使用list(range(1,6))得到数字list\n",
    "+ len()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['韩梅梅', '李雷', '林涛', 'Jim', 'Kate', 'Lucy']\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 1, 'b', 2, 'c', 3, True, False]\n"
     ]
    }
   ],
   "source": [
    "num_str_bool = ['a', 1, 'b', 2, 'c', 3, True, False]\n",
    "print(num_str_bool)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 列表解析式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]\n"
     ]
    }
   ],
   "source": [
    "a=[x for x in range(101) if x%3==0]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 常见列表操作\n",
    "+ 读取一个列表的任意一个项目(item)：listname[i]\n",
    "+ 读取一个列表中若干个项目(item)：listname[i:j]\n",
    "+ 遍历一个列表的每个项目(item)：for 结构\n",
    "+ 改变list中item的值：listname[i] = newvalue\n",
    "+ 在列表中找一个item：value in listname\n",
    "+ 在列表中增加一个item：listname.append()和list.extend()\n",
    "+ 在列表中删除一个item：del listname[i]、listname.remove(item)和listname.pop()\n",
    "+ 对列表进行排序：listname.sort()和sorted(listname)\n",
    "+ 列表顺序的反转：listname.reverse()\n",
    "+ 列表的复制：=和listname.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "韩梅梅\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "student = students[0]\n",
    "print(student)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "前3个同学: ['韩梅梅', '李雷', '林涛']\n"
     ]
    }
   ],
   "source": [
    "print('前3个同学:', students[0:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "韩梅梅\n",
      "李雷\n",
      "林涛\n",
      "Jim\n",
      "Kate\n",
      "Lucy\n"
     ]
    }
   ],
   "source": [
    "for student in students:\n",
    "    print(student)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['韩梅梅', '李雷', '林涛', 'Jim', '杨洋', 'Lucy']\n"
     ]
    }
   ],
   "source": [
    "students[4] = '杨洋'\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print('杨洋' in students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['韩梅梅', '李雷', '林涛', 'Jim', '杨洋', 'Lucy', '王晨']\n"
     ]
    }
   ],
   "source": [
    "students.append('王晨')\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [4, 5, 6]]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=[1,2,3]\n",
    "b=[4,5,6]\n",
    "a.append(b)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=[1,2,3]\n",
    "a.extend(b)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['韩梅梅', '李雷', '林涛', 'Jim', '杨洋', 'Lucy', '王晨', '陈粒', '程璧', 'T', 'a', 'y', 'l', 'o', 'r', '花', '粥', '谢春花', '陈粒', '程璧', 'T', 'a', 'y', 'l', 'o', 'r', '花', '粥', '谢春花']\n"
     ]
    }
   ],
   "source": [
    "students.extend(['陈粒','程璧'])\n",
    "students.extend('Taylor')\n",
    "students.extend('花粥')\n",
    "students.extend(['谢春花'])\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['李雷', '林涛', 'Jim', '杨洋', 'Lucy', '王晨', '陈粒', '程璧', 'T', 'a', 'y', 'l', 'o', 'r', '花', '粥', '谢春花', '陈粒', '程璧', 'T', 'a', 'y', 'l', 'o', 'r', '花', '粥', '谢春花']\n"
     ]
    }
   ],
   "source": [
    "del students[0]\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['李雷', '林涛', '杨洋', 'Lucy', '王晨', '陈粒', '程璧', 'T', 'a', 'y', 'l', 'o', 'r', '花', '粥', '谢春花', '陈粒', '程璧', 'T', 'a', 'y', 'l', 'o', 'r', '花', '粥', '谢春花']\n"
     ]
    }
   ],
   "source": [
    "students.remove('Jim')\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lucy\n",
      "['韩梅梅', '李雷', '林涛', 'Jim', 'Kate']\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "last_stu = students.pop()\n",
    "\n",
    "print(last_stu)\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jim\n",
      "Kate\n",
      "Lucy\n",
      "李雷\n",
      "林涛\n",
      "韩梅梅\n",
      "['Jim', 'Kate', 'Lucy', '李雷', '林涛', '韩梅梅']\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "students.sort()\n",
    "for student in students:\n",
    "    print(student)\n",
    "    \n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jim\n",
      "Kate\n",
      "Lucy\n",
      "李雷\n",
      "林涛\n",
      "韩梅梅\n",
      "['韩梅梅', '李雷', '林涛', 'Jim', 'Kate', 'Lucy']\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "for student in sorted(students, reverse=False):\n",
    "    print(student)\n",
    "    \n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Lucy', 'Kate', 'Jim', '林涛', '李雷', '韩梅梅']\n"
     ]
    }
   ],
   "source": [
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "students.reverse()\n",
    "\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 2, 3, 5, 8, 13, 21]\n",
      "[1, 1, 2, 3, 5, 8, 13, 21]\n",
      "[1, 1, 2, 3, 5, 8, 13, 21, 9999]\n",
      "[1, 1, 2, 3, 5, 8, 13, 21, 9999]\n",
      "[1, 1, 2, 3, 5, 8, 13, 21]\n"
     ]
    }
   ],
   "source": [
    "fibonacci_numbers = [1, 1, 2, 3, 5, 8, 13, 21]  #斐波那契数\n",
    "aa = fibonacci_numbers\n",
    "print(aa)\n",
    "\n",
    "bb = fibonacci_numbers.copy()\n",
    "print(bb)\n",
    "\n",
    "fibonacci_numbers.append(9999)\n",
    "print(fibonacci_numbers)\n",
    "print(aa)                       # 等号的话，值会传递\n",
    "print(bb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元组\n",
    "\n",
    "+ 元组使用()创立，列表使用[]创立\n",
    "+ 元组不能修改item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n",
      "age\n",
      "30\n"
     ]
    }
   ],
   "source": [
    "tuple_example = ('age', 30) # 只读，有序\n",
    "print(type(tuple_example))\n",
    "print(tuple_example[0])\n",
    "print(tuple_example[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典\n",
    "\n",
    "+ 字典以一种联系的方式储存信息，字典储存的是键值对（\"key-value\" pairs) ，其中任意一条信息至少和另外一条信息相联系。\n",
    "+ 遍历一个字典的键值对：for 结构 + mydict.items()\n",
    "+ 遍历一个字典的key：for 结构 + mydict\n",
    "+ 遍历一个字典的value：for结构 + mydict.values()\n",
    "+ 改变字典中的key：添加正确的——删除错误的\n",
    "+ 改变字典中的value：dictname['key']='newvalue'\n",
    "+ 在字典中增加键值对：成对添加\n",
    "+ 在字典中删除键值对：del dictname['key']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dictionary_name = {key_1: value_1,\n",
    "                   key_2: value_2,\n",
    "                   key_3: value_3,\n",
    "                   }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Key: key_1\n",
      "Value: value_1\n",
      "\n",
      "Key: key_2\n",
      "Value: value_2\n",
      "\n",
      "Key: key_3\n",
      "Value: value_3\n"
     ]
    }
   ],
   "source": [
    "# 以.item()函数把键值对变成了元组构成的列表\n",
    "# mydict.items()\n",
    "my_dict = {'key_1': 'value_1',\n",
    "    'key_2': 'value_2',\n",
    "    'key_3': 'value_3',\n",
    "    }\n",
    "\n",
    "for key, value in my_dict.items():\n",
    "    print('\\nKey: %s' % key)\n",
    "    print('Value: %s' % value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_dict = {'key_1': 'value_1',\n",
    "    'key_2': 'value_2',\n",
    "    'key_3': 'value_3',\n",
    "    }\n",
    "\n",
    "for key in my_dict:\n",
    "    print('Key: %s' % key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_dict = {'key_1': 'value_1',\n",
    "    'key_2': 'value_2',\n",
    "    'key_3': 'value_3',\n",
    "    }\n",
    "\n",
    "for value in my_dict.values():\n",
    "    print('Value: %s' % value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_words = {'lisst': 'A collection of values that are not connected, but have an order.',\n",
    "                'dictionary': 'A collection of key-value pairs.',\n",
    "                'function': 'A named set of instructions that defines a set of actions in Python.',\n",
    "                }\n",
    "\n",
    "# Create a new, correct key, and connect it to the old value.\n",
    "#  Then delete the old key.\n",
    "python_words['list'] = python_words['lisst']\n",
    "\n",
    "for word, meaning in python_words.items():\n",
    "    print(\"\\nWord: %s\" % word)\n",
    "    print(\"Meaning: %s\" % meaning)\n",
    "\n",
    "del python_words['lisst']\n",
    "\n",
    "# Print the dictionary, to show that the key has changed.\n",
    "for word, meaning in python_words.items():\n",
    "    print(\"\\nWord: %s\" % word)\n",
    "    print(\"Meaning: %s\" % meaning)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_words = {'list': 'A collection of values that are not connected, but have an order.',\n",
    "                'dictionary': 'A collection of key-value pairs.',\n",
    "                'function': 'A named set of instructions that defines a set of actions in Python.',\n",
    "                }\n",
    "\n",
    "print('dictionary: ' + python_words['dictionary'])\n",
    "    \n",
    "# Clarify one of the meanings.\n",
    "python_words['dictionary'] = 'A collection of key-value pairs. Each key can be used to access its corresponding value.'\n",
    "\n",
    "print('\\ndictionary: ' + python_words['dictionary'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_words = {}\n",
    "\n",
    "# Fill the dictionary, pair by pair.\n",
    "python_words['list'] ='A collection of values that are not connected, but have an order.'\n",
    "python_words['dictionary'] = 'A collection of key-value pairs.'\n",
    "python_words['function'] = 'A named set of instructions that defines a set of actions in Python.'\n",
    "\n",
    "# Print out the items in the dictionary.\n",
    "for word, meaning in python_words.items():\n",
    "    print(\"\\nWord: %s\" % word)\n",
    "    print(\"Meaning: %s\" % meaning)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_words = {'list': 'A collection of values that are not connected, but have an order.',\n",
    "                'dictionary': 'A collection of key-value pairs.',\n",
    "                'function': 'A named set of instructions that defines a set of actions in Python.',\n",
    "                }\n",
    "\n",
    "# Show the current set of words and meanings.\n",
    "print(\"\\n\\nThese are the Python words I know:\")\n",
    "for word, meaning in python_words.items():\n",
    "    print(\"\\nWord: %s\" % word)\n",
    "    print(\"Meaning: %s\" % meaning)\n",
    "    \n",
    "# Remove the word 'list' and its meaning.\n",
    "del python_words['list']\n",
    "\n",
    "# Show the current set of words and meanings.\n",
    "print(\"\\n\\nThese are the Python words I know:\")\n",
    "for word, meaning in python_words.items():\n",
    "    print(\"\\nWord: %s\" % word)\n",
    "    print(\"Meaning: %s\" % meaning)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 集合\n",
    "+ 集合的元素是无序的，以大括号创建，item以逗号分隔\n",
    "+ 集合的元素不能重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "<class 'set'>\n"
     ]
    }
   ],
   "source": [
    "set_example1 = {'age':30}\n",
    "set_example2 = {'age', 30}  # 无序，可写\n",
    "print(type(set_example1))\n",
    "print(type(set_example2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 1, 3]\n",
      "{1, 2, 3}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "li = [1,2,1,3]             # 列表\n",
    "print(li)\n",
    "\n",
    "se = {1,2,1,3}             # 集合\n",
    "print(se)\n",
    "list(se)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文件操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### with结构的文件打开方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('pi_digits.txt') as file_object:\n",
    "    lines = file_object.readlines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['3.1415926535\\n', '  8979323846\\n', '  2643383279\\n']"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lines"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### read一族函数\n",
    "\n",
    "+ read() 每次读取整个文件，它通常用于将文件内容放到一个字符串变量中。\n",
    "    + 如果文件大于可用内存，为了保险起见，可以反复调用read(size)方法，每次最多读取size个字节的内容。\n",
    "+ readlines() 自动将文件内容分析成一个行的列表.\n",
    "    + 该列表可以由 Python 的 for ... in ... 结构进行处理。\n",
    "+ readline() 每次只读取一行，通常比readlines() 慢得多。\n",
    "    + 仅当没有足够内存可以一次读取整个文件时，才应该使用 readline()。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = 'pi_digits.txt'\n",
    "\n",
    "#read()\n",
    "with open(filename) as file_object:\n",
    "    lines = file_object.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.1415926535\n",
      "  8979323846\n",
      "  2643383279\n",
      "\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(lines)\n",
    "print(type(lines))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "# readlines\n",
    "with open(filename) as file_object:\n",
    "    lines = file_object.readlines()   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['3.1415926535\\n', '  8979323846\\n', '  2643383279\\n']\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "print(lines)\n",
    "print(type(lines)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(filename) as file_object:\n",
    "    lines = file_object.readline()    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.1415926535\n",
      "\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(lines)\n",
    "print(type(lines))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.1415926535\n",
      "  8979323846\n",
      "  2643383279\n"
     ]
    }
   ],
   "source": [
    "with open(filename) as file_object:\n",
    "    lines = file_object.readlines()     \n",
    "    \n",
    "    \n",
    "for line in lines:\n",
    "    print(line.rstrip()) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 添加内容\n",
    "\n",
    "+ write()是将字符串写入文件\n",
    "+ writelines()是对列表的操作，把一个字符串列表添加到文件中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = 'programming.txt'\n",
    "with open(filename, 'a') as file_object:\n",
    "    file_object.write(\"I also love finding meaning in large datasets.\\n\")\n",
    "    file_object.write(\"I also love creating apps that can run in a browser.\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.1415926535\n",
      "  8979323846\n",
      "  2643383279\n",
      "I also love finding meaning in large datasets.\n",
      "I also love creating apps that can run in a browser.\n",
      "I also love finding meaning in large datasets.\n",
      "I also love creating apps that can run in a browser.\n"
     ]
    }
   ],
   "source": [
    "with open(filename) as file_object:\n",
    "    lines = file_object.readlines()  \n",
    "for line in lines:\n",
    "    print(line.rstrip())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = 'programming.txt'\n",
    "\n",
    "with open(filename, 'a') as file_object:\n",
    "    file_object.writelines([\"I also love finding meaning in large datasets.\\n\",\n",
    "                            \"I also love creating apps that can run in a browser.\\n\"])\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.1415926535\n",
      "  8979323846\n",
      "  2643383279\n",
      "I also love finding meaning in large datasets.\n",
      "I also love creating apps that can run in a browser.\n",
      "I also love finding meaning in large datasets.\n",
      "I also love creating apps that can run in a browser.\n",
      "I also love finding meaning in large datasets.\n",
      "I also love creating apps that can run in a browser.\n"
     ]
    }
   ],
   "source": [
    "with open(filename) as file_object:\n",
    "    lines = file_object.readlines()  \n",
    "for line in lines:\n",
    "    print(line.rstrip())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 生成新文件或覆盖旧文件内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('test.txt', 'w') as wt:\n",
    "    wt.write('Hello, world!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('test.txt', 'w') as wt:\n",
    "    wt.write('Hello, Python!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "563px",
    "left": "731px",
    "top": "158px",
    "width": "216px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
